﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbConnect;
using DbConnect.Sql;

namespace DnDEngine.Class
{
    public class SiteType
    {

         #region "Private attributes"


        #endregion

        #region "Properties"

        public int ID
        {
            get;
            set;
        }

        public String Lib
        {
            get;
            set;
        }

        #endregion

        #region "Events"

        #endregion

        #region "Public functions"

        public SiteType(String SiteTypeLib)
        {

            try
            {
                Logger.Log.TraceAction("SiteType.Constructor", SiteTypeLib);
                Lib = SiteTypeLib;
                Insert();
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("SiteType.Constructor", ex);

            }
            finally
            {
            }

        }

        public SiteType(int IDSiteType)
        {

            try
            {
                Logger.Log.TraceAction("SiteType.Constructor", IDSiteType.ToString());
                ID = IDSiteType;
                Load();
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("SiteType.Constructor", ex);

            }
            finally
            {
            }

        }

        #endregion

        #region "Private functions"

        private int Insert()
        {

            var Ok = 0;
            try
            {
                Logger.Log.TraceAction("SiteType.Insert", "");
                var data = new DbConnect.DataAccess();
                var req = SqlSiteType.InsertInto;
                data.AddParam(ParamSiteType.Lib, this.Lib);

                Ok = data.Insert(req);

                this.ID = data.GetLastIDInserted(SqlSiteType.TableName);
                data = null;
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("SiteType.Insert", ex);
                Ok = 0;
            }
            finally
            {
            }
            return Ok;
        }

        private void Load()
        {


            try
            {
                Logger.Log.TraceAction("SiteType.Load", "");
                var data = new DbConnect.DataAccess();
                data.AddParam(ParamSiteType.ID, ID);
                var req = SqlRequest.Select + SqlSiteType.Lib + SqlRequest.From + SqlSiteType.TableName +
                    SqlRequest.Where + SqlSiteType.ID + SqlRequest.Equal + ID;
                var table = data.Select(req);

                if (table != null && table.Rows.Count > 0)
                {
                    Lib = table.Rows[0].GetStringValue("SiteTypeLib");
                }
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("SiteType.Load", ex);

            }
            finally
            {
            }

        }

        private int Delete()
        {

            var Ok = 0;
            try
            {
                Logger.Log.TraceAction("SiteType.Insert", "");
                var data = new DbConnect.DataAccess();
                var req = SqlSiteType.Delete;
                data.AddParam(ParamSiteType.ID, ID);

                Ok = data.Delete(req);
                data = null;
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("SiteType.Insert", ex);
                Ok = 0;
            }
            finally
            {
            }
            return Ok;
        }

        private int Update()
        {

            var Ok = 0;
            try
            {
                Logger.Log.TraceAction("SiteType.Insert", "");
                var data = new DbConnect.DataAccess();
                var req = SqlSiteType.Update;
                data.AddParam(ParamSiteType.ID, ID);
                data.AddParam(ParamSiteType.Lib, Lib);

                Ok = data.Update(req);
                data = null;
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("SiteType.Insert", ex);
                Ok = 0;
            }
            finally
            {
            }
            return Ok;
        }

        #endregion
		 
		 
		 
    }
}
